#ifndef WALL_AVOIDANCE_BEHAVIOUR_H_
#define WALL_AVOIDANCE_BEHAVIOUR_H_

#include "BehaviourI.h"
#include <LinearMath/btTransform.h>
#include <float.h>  
/* Iterates over a vector of actors, and any that are static rectangles it
  treats as walls. Can avoid completely, or cli[ based on parameters
*/
class WallAvoidanceBehaviour : public BehaviourI
{
public:
    WallAvoidanceBehaviour(): m_wander(NULL){}
    ~WallAvoidanceBehaviour(){if (m_wander) delete m_wander;}
    virtual void CalcMotion(float* motion, BehaviourInfo* info, float weighting);
    void SetSecondaryBeahviour(BehaviourI* i){m_wander = i;}
/** Intersection Tests **/
private:
    float Get2DTriangleArea(const btVector3& pA, const btVector3& pB, const btVector3& pC);
    bool LineSegTest(const btVector3& pA, const btVector3& pB,  //line1
                     const btVector3& pC, const btVector3& pD,  //line2
                     btVector3& p);                    //params 
    btVector3 CalculateNormalWall(int index, const btTransform& matrix);
private:

    struct WallAvoidCache
    {
        WallAvoidCache() : bodyIndex(-1),
                            probeIndex(0),
            smallestDistance(FLT_MAX),
            positionWorld(0,0,0)
        {}
        float smallestDistance;
        btVector3 positionWorld;
        btVector3 wallNormal;
        int bodyIndex;
        int probeIndex;
    };

    BehaviourI* m_wander;
};
#endif